﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
namespace DALL.Procurement
{
    public class T_PUB_WAREHOUSING
    {
        public T_PUB_WAREHOUSING()
        { }

        /// <summary>
        /// 获取采购入库主题
        /// </summary>
        /// <param name="branch_id"></param>
        /// <returns></returns>
        public DataSet GetPurchaseInApplyTitel(int branch_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITEL FROM T_PUB_WAREHOUSING ");
            strSql.Append(" where BRANCH_ID=" + branch_id);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetPurchaseInApplyTitel()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITEL FROM T_PUB_WAREHOUSING ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetApplyList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select WAREHOUSING_ID,a.BRANCH_ID,BRANCH_NAME,a.LIBRARY_ID,LIBRARY_NAME,APPLICANT,a.REMARK,TITEL,TIME,APPLICANT_NAME  ");
            strSql.Append(" FROM T_PUB_WAREHOUSING as a, T_PUB_LIBRARY as b, T_PUB_BRANCH as c");
            strSql.Append(" where  b.LIBRARY_ID = a.LIBRARY_ID and c.BRANCH_ID = a.BRANCH_ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        #region  Method


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Procurement.T_PUB_WAREHOUSING model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_WAREHOUSING(");
            strSql.Append("WAREHOUSING_ID,TIME,APPLICANT,APPLICANT_NAME,BRANCH_ID,LIBRARY_ID,REMARK,TITEL)");

            strSql.Append(" values (");
            strSql.Append("@WAREHOUSING_ID,@TIME,@APPLICANT,@APPLICANT_NAME,@BRANCH_ID,@LIBRARY_ID,@REMARK,@TITEL)");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "WAREHOUSING_ID", DbType.String, model.WAREHOUSING_ID);
            db.AddInParameter(dbCommand, "TIME", DbType.DateTime, model.TIME);
            db.AddInParameter(dbCommand, "APPLICANT", DbType.Int32, model.APPLICANT);
            db.AddInParameter(dbCommand, "APPLICANT_NAME", DbType.String, model.APPLICANT_NAME);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, model.LIBRARY_ID);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "TITEL", DbType.String, model.TITEL);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Procurement.T_PUB_WAREHOUSING model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_WAREHOUSING set ");
            strSql.Append("TIME=@TIME,");
            strSql.Append("APPLICANT=@APPLICANT,");
            strSql.Append("APPLICANT_NAME=@APPLICANT_NAME,");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("LIBRARY_ID=@LIBRARY_ID,");
            strSql.Append("REMARK=@REMARK,");
            strSql.Append("TITEL=@TITEL");
            strSql.Append(" where WAREHOUSING_ID=@WAREHOUSING_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "WAREHOUSING_ID", DbType.String, model.WAREHOUSING_ID);
            db.AddInParameter(dbCommand, "TIME", DbType.DateTime, model.TIME);
            db.AddInParameter(dbCommand, "APPLICANT", DbType.Int32, model.APPLICANT);
            db.AddInParameter(dbCommand, "APPLICANT_NAME", DbType.String, model.APPLICANT_NAME);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "LIBRARY_ID", DbType.Int32, model.LIBRARY_ID);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "TITEL", DbType.String, model.TITEL);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string WAREHOUSING_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_WAREHOUSING ");
            strSql.Append(" where WAREHOUSING_ID=@WAREHOUSING_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "WAREHOUSING_ID", DbType.String, WAREHOUSING_ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Procurement.T_PUB_WAREHOUSING GetModel(string WAREHOUSING_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select WAREHOUSING_ID,TIME,APPLICANT,APPLICANT_NAME,BRANCH_ID,LIBRARY_ID,REMARK,TITEL from T_PUB_WAREHOUSING ");
            strSql.Append(" where WAREHOUSING_ID=@WAREHOUSING_ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "WAREHOUSING_ID", DbType.String, WAREHOUSING_ID);
            Model.Procurement.T_PUB_WAREHOUSING model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select WAREHOUSING_ID,TIME,APPLICANT,APPLICANT_NAME,BRANCH_ID,LIBRARY_ID,REMARK,TITEL ");
            strSql.Append(" FROM T_PUB_WAREHOUSING ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Procurement.T_PUB_WAREHOUSING> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select WAREHOUSING_ID,TIME,APPLICANT,APPLICANT_NAME,BRANCH_ID,LIBRARY_ID,REMARK,TITEL ");
            strSql.Append(" FROM T_PUB_WAREHOUSING ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Procurement.T_PUB_WAREHOUSING> list = new List<Model.Procurement.T_PUB_WAREHOUSING>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Procurement.T_PUB_WAREHOUSING ReaderBind(IDataReader dataReader)
        {
            Model.Procurement.T_PUB_WAREHOUSING model = new Model.Procurement.T_PUB_WAREHOUSING();
            object ojb;
            model.WAREHOUSING_ID = dataReader["WAREHOUSING_ID"].ToString();
            ojb = dataReader["TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TIME = (DateTime)ojb;
            }
            ojb = dataReader["APPLICANT"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPLICANT = (int)ojb;
            }
            model.APPLICANT_NAME = dataReader["APPLICANT_NAME"].ToString();
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            ojb = dataReader["LIBRARY_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LIBRARY_ID = (int)ojb;
            }
            model.REMARK = dataReader["REMARK"].ToString();
            model.TITEL = dataReader["TITEL"].ToString();
            return model;
        }

        #endregion  Method
    }
}
